package com.weakteam.orderonline.adminconsole.gwt.client.design;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Category;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;
import com.weakteam.orderonline.adminconsole.gwt.client.widget.*;


/**
 * Created by Max on 03.04.2014.
 */
public class MenuPage extends Composite {
    interface MenuPageUiBinder extends UiBinder<Widget, MenuPage> { }
    private static MenuPageUiBinder uiBinder = GWT.create(MenuPageUiBinder.class);

    private static MenuPage instance = null;
    public static MenuPage getInstance() {
        if (instance == null) {
            instance = new MenuPage();
        }
        return instance;
    }

    @UiField  protected Label menuItemRoot;
    @UiField  protected AbsolutePanel menuSpace;
    protected MenuItemContainer menuItemContainer;

    @UiField  protected FlowPanel contentPanel;
    protected MenuCategoryWidget categoryWidget;
    protected MenuGoodsWidget goodsWidget;

    @UiFactory
    Header makeHeader() { // method name is insignificant
        return Header.getInstance();
    }
//    @UiFactory
//    MenuGoodsControl makeMenuContentControl() { // method name is insignificant
//        return new MenuGoodsControl();
//    }

    private MenuPage() {
        // initialize
        initWidget(uiBinder.createAndBindUi(this));
        this.menuItemContainer = new MenuItemContainer(menuSpace);
        this.categoryWidget = new MenuCategoryWidget();
        this.goodsWidget = new MenuGoodsWidget();
        // add handlers
        this.menuItemRoot.addClickHandler (new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MenuPage.this.showCategoryWidget();
            }
        });
        this.menuItemContainer.addSelectionHandler (new SelectionHandler<MenuItemBox>() {
            @Override
            public void onSelection(SelectionEvent<MenuItemBox> event) {
                showGoodsWidget(event.getSelectedItem().getCategory());
            }
        });
        this.categoryWidget.addOpenHandler (new OpenHandler<Category>() {
            @Override
            public void onOpen(OpenEvent<Category> event) {
                menuItemContainer.setBoxSelectedByMenuId(event.getTarget().getId());
            }
        });
        this.categoryWidget.addValueChangeHandler (new ValueChangeHandler<JSONArray>() {
            @Override
            public void onValueChange(ValueChangeEvent<JSONArray> event) {
                menuItemContainer.setLiveData(event.getValue());
            }
        });
        // set initial state
        this.menuSpace.add(menuItemContainer);
        this.showCategoryWidget();

    }

    public void showCategoryWidget() {
        this.categoryWidget.prepare();
        //
        this.menuItemRoot.addStyleName("menu-item-selected");
        this.contentPanel.clear();
        this.contentPanel.add(this.categoryWidget);
    }

    public void showGoodsWidget(Category category) {
        // clear old data
        this.goodsWidget.prepare(category);
        // request new data
        Requester request = new Requester();
        request.addResponser(this.goodsWidget.goodsContainer);
        request.getGoodsAll(category.getId());
        //
        this.menuItemRoot.removeStyleName("menu-item-selected");
        this.contentPanel.clear();
        this.contentPanel.add(this.goodsWidget);
    }


}
